﻿
var hdnUpdatePriority = $("#hdnUpdatePriority").val();
var hdnUserID = $("#hdnUserID").val();

jQuery(document).ready(function () {
        
    AppMonthlyAlerts();
});

function AppMonthlyAlerts()
{
    if (($("#app_mo_alert_badge").val() == '') || ($("#app_mo_alert_badge").val() == '0')) {
        $("#chkMob_BadgeAlert_WebView").attr('checked', false);
    }
    else {
        $("#chkMob_BadgeAlert_WebView").attr('checked', true);
    }


    if (($("#app_mo_alert_popup").val() == '') || ($("#app_mo_alert_popup").val() == '0')) {
        $("#chkMob_PopupAlert_WebView").attr('checked', false);
    }
    else {
        $("#chkMob_PopupAlert_WebView").attr('checked', true);
    }

    if (($("#web_mo_alert_email").val() == '')) {
        $("#chkweb_mo_alert_email_WebView").attr('checked', false);
    }
    else {
        $("#chkweb_mo_alert_email_WebView").attr('checked', true);
    }

}

//Web View Ajax Functions
$("#getWeekoftheMonth_WebView, #getDayoftheWeek_WebView, #getTimeoftheDay_WebView").live("click", function (e) {
    $("#update").hide();
});

$("#getWeekoftheMonth_WebView").live("change", function (e) {

    var user_id = hdnUserID;
    var Priority_ValueUpdate = $(this).val();
    var ConditionSet = "Week";
    if (user_id != '') {
        UpdateMonthlyAlerts(user_id, Priority_ValueUpdate, ConditionSet);
    }
    $("#userchange_app_mo_alert_week").val(Priority_ValueUpdate);
    window.scrollTo(0, 0);

});

$("#getDayoftheWeek_WebView").live("change", function (e) {

    var user_id = hdnUserID;
    var Priority_ValueUpdate = $(this).val();
    var ConditionSet = "Day";
    if (user_id != '') {
        UpdateMonthlyAlerts(user_id, Priority_ValueUpdate, ConditionSet);
    }

    $("#userchange_app_mo_alert_day").val(Priority_ValueUpdate);
    window.scrollTo(0, 0);
});

$("#getTimeoftheDay_WebView").live("change", function (e) {

    var user_id = hdnUserID;
    var Priority_ValueUpdate = $(this).val();
    var ConditionSet = "Time";
    if (user_id != '') {
        UpdateMonthlyAlerts(user_id, Priority_ValueUpdate, ConditionSet);
    }

    $("#userchange_app_mo_alert_hour").val(Priority_ValueUpdate);
    window.scrollTo(0, 0);

});

function UpdateMonthlyAlerts(user_id, Priority_ValueUpdate, ConditionSet) {

    $("#wait").show();
    $.ajax({
        type: "POST",
        url: hdnUpdatePriority,
        data: { "user_id": user_id, "PriorityValueUpdate": Priority_ValueUpdate, "Condition": ConditionSet },
    success: function (data) {
        if (data == 1) {
            $("#update").show();

            var ConditionSetMessage = "";
            if (ConditionSet == "Badge")
            {
                ConditionSetMessage="Badge";
            }
            else if (ConditionSet == "Alerts")
            {
                ConditionSetMessage="Alerts";
            }
            else if (ConditionSet == "Week")
            {
                ConditionSetMessage = "Week of the month";
            }
            else if (ConditionSet == "Day") {

                ConditionSetMessage = "Day of that week";
            }
            else if (ConditionSet == "Time") {
                ConditionSetMessage = "Time of that day";
            }

            $("#update label").html(ConditionSetMessage+ " updated");
                      
        }
        else {
            $("#update").hide();
        }
        $("#wait").hide();
    },
    error: function (xhr, ajaxOptions, thrownError) {
        alert('Failed to retrieve states.');

    }
});
}

$("#chkMob_BadgeAlert_WebView").live("click", function (e) {

    var user_id = hdnUserID;
    var Priority_ValueUpdate = 0;

    if ($(this).is(':checked')) {
        Priority_ValueUpdate = 1;
    }
    else {
        Priority_ValueUpdate = 0;
    }

    var ConditionSet = "Badge";
    if (user_id != '') {
        UpdateMonthlyAlerts(user_id, Priority_ValueUpdate, ConditionSet);
    }

    if ($(this).is(':checked')) {

        $("#userchange_app_mo_alert_badge").val(1);
    }
    else {
        $("#userchange_app_mo_alert_badge").val(0);
    }

});

$("#chkMob_PopupAlert_WebView").live("click", function (e) {

    var user_id = hdnUserID;
    var Priority_ValueUpdate = 0;

    if ($(this).is(':checked')) {
        Priority_ValueUpdate = 1;
    }
    else {
        Priority_ValueUpdate = 0;
    }

    var ConditionSet = "Alerts";
    if (user_id != '') {
        UpdateMonthlyAlerts(user_id, Priority_ValueUpdate, ConditionSet);
    }

    if ($(this).is(':checked')) {

        $("#userchange_app_mo_alert_popup").val(1);
    }
    else {
        $("#userchange_app_mo_alert_popup").val(0);
    }

});


//WebView Personalization
$('#moweb_mo_alert_email_WebView').live("keyup", function (e) {

    if (e.which == 8 && this.value.length == 0) {
        $("#chkweb_mo_alert_email_WebView").attr('checked', false);

        $("#moweb_mo_alert_email_WebView").val('');
         

    }
});

//WebView Personalization
$("#moweb_mo_alert_email_WebView").live("keypress", function (e) {

    //If user starts to type in email textbox,then radio button for “Email” becomes active
    $("#chkweb_mo_alert_email_WebView").attr('checked', true);

});

//WebView Personalization
$("#chkweb_mo_alert_email_WebView").live("click", function (e) {

    if ($(this).is(':checked')) {
    }
    else {
        $("#moweb_mo_alert_email_WebView").val('');
    }

});


//WebView Monthly Alerts
$("#moweb_mo_alert_email_WebView").live("blur", function (e) {

    //check that email format  is correct
    if ($(this).val() != '') {
        var returnValue = isValidEmailAddress($(this).val());
        if (returnValue == false) {
            alert("Email address is not in the proper format for a valid email address.");
        }
         
    }
});

